import Util from "../utils/Util";
export default {
    inject: {
        appInfo: {
            from: 'appInfo',
            default: function () {
                return { appId: '0', envs: {} }
            }
        }
    },
    data() {
        return {
            callDb: ''
        }
    },
    computed: {
        perviewUrl () {
            return Util.mergeUrl(process.env.VUE_APP_BASE_API, '/resource/preview/')
        },
        downLoadUrl () {
            return Util.mergeUrl(process.env.VUE_APP_BASE_API, '/resource/download/')
        }
    },
    methods: {
        mergeReportUrl(url) {
            let newUrl = url + ( url.includes('?') ? '&appId=' : '?appId=') + this.appInfo.appId
            if (this.callDb) {
                newUrl += '&callDb=' + callDb
            }
            newUrl += '&access_token=' + this.$AuthStorage.getAccessToken()
            return newUrl
        },
        createReport(body) {
            return new Promise(resolve => {
                this.$post(this.mergeReportUrl('/appInfo/createReport'), body)
                    .then(res => {
                        this.$resultMsg(this, res)
                        if (res.code === 200 && res.data && res.data.length > 0) {
                            for (let i = 0; i < res.data.length; i++) {
                                this.downloadReport(res.data[i])
                            }
                        }
                        resolve(res)
                    })
            })
        },
        signReport(body) {
            return new Promise(resolve => {
                this.$post(this.mergeReportUrl('/appInfo/signReport'), body)
                    .then(res => {
                        this.$resultMsg(this, res)
                        if (res.code === 200 && res.data) {
                            this.perviewReport(res.data)
                        }
                        resolve(res)
                    })
            })
        },
        cancelReport(row) {

        },
        perviewReport (row) {
            if (row.resourceId) {
                window.open(this.mergeReportUrl(this.perviewUrl + row.resourceId))
            }  else {
                this.download(row)
            }
        },
        downloadReport (row) {
            if (row.storeUrl && (row.storeUrl.includes('http://') || row.storeUrl.includes('https://'))) {
                window.open(row.storeUrl)
            } else if (row.resourceId) {
                window.open(this.mergeReportUrl(this.downLoadUrl + row.resourceId))
            } else if (row.base64) {
                const mime = this.getMime(row.resourceSuffix)
                let raw = window.atob(row.base64) // 解码base64得到二进制字符串
                let rawLength = raw.length
                let uInt8Array = new Uint8Array(rawLength) // 创建8位无符号整数值的类型化数组
                for (let i = 0; i < rawLength; ++i) {
                    uInt8Array[i] = raw.charCodeAt(i) // 数组接收二进制字符串
                }
                const blob = new Blob([uInt8Array], {type: mime})
                const downloadElement = document.createElement('a')
                let href = blob
                if (typeof blob === 'string') {
                    downloadElement.target = '_blank'
                } else {
                    href = window.URL.createObjectURL(blob) // 创建下载的链接
                }
                downloadElement.href = href
                downloadElement.download = row.resourceName // 下载后文件名
                document.body.appendChild(downloadElement)
                downloadElement.click() // 触发点击下载
                document.body.removeChild(downloadElement) // 下载完成移除元素
                if (typeof blob !== 'string') {
                    window.URL.revokeObjectURL(href) // 释放掉blob对象
                }
            } else {
                this.$resultMsg(this, { msg: '不支持的操作'})
            }
        },
        queryDown(queryBody) {

        },
        getMime(type) {
            switch (type) {
                case 'txt': return 'text/plain';
                case 'doc': return 'application/msword';
                case 'docx': return 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
                case 'xls': return 'application/vnd.ms-excel';
                case 'xlsx': return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
                case 'pdf': return 'application/pdf';
                case 'pptx': return 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
                case 'ppt': return 'application/vnd.ms-powerpoint';
                case 'png': return 'image/png';
                case 'jpg': return 'image/jpeg';
                case 'gif': return 'image/gif';
                case 'svg': return 'image/svg+xml';
                case 'ico': return 'image/x-icon';
                case 'bmp': return 'image/bmp,';
            }
        }
    }
}
